#pragma strict

var groundLine : GameObject;

private var scriptsGObj : GameObject;
var lineTransformList : List.<GameObject>;

private var BZ3UtilsComponent : BZ3Utils;

private var groundMagnitude : Vector3 = Vector3(1000,100,1000);
private var x : float = 1000;
private var y : float = 100;
private var z : float = 1000;

private var spawnPositionLineY : float = -2;
private var spawnPositionLine : Vector3;
private var spawnRotationLine : Quaternion;

private var outOfRangeDistance : int = 500;
private var randomRangePos : float = 400;
private var randomRangeNeg : float = -400;
private var createDeadZoneNeg : float = -20;
private var createDeadZonePos : float = 20;

private var deadZoneNeg : float = -300;
private var deadZonePos : float = 300;

private var amount : int = 50;


function Start () {
	scriptsGObj = GameObject.FindGameObjectWithTag("Scripts");
	
	BZ3UtilsComponent = scriptsGObj.GetComponent(BZ3Utils);

	transform.localScale = groundMagnitude;
	
	createLines(scriptsGObj.transform);
	
	renderer.material.shader = Shader.Find("Self-Illumin/Diffuse");
	renderer.material.SetColor ("_Color", Color.black);
    renderer.material.SetColor ("_SpecColor", Color.black);
}

function Update () {
	x+=5;
	z+=5;
	var posPlayer : Vector3 = GameObject.FindGameObjectWithTag("Scripts").transform.position;
	groundMagnitude.Set(x,y,z);
	//Increase the Ground Magnitude/Scale
	//this.gameObject.transform.localScale = groundMagnitude;
	transform.position = Vector3(posPlayer.x,-52.0,posPlayer.z);
	
	for(var lineTmp : GameObject in lineTransformList){
		var distance : float = Vector3.Distance(lineTmp.transform.position, scriptsGObj.transform.position);
		
		if(distance > outOfRangeDistance){
			RepositionLines(lineTmp, scriptsGObj.transform);
		}
	}
}

public function createLines(trans : Transform){
	for(var i=0; i<amount;i++){
		spawnPositionLine = Vector3(trans.position.x + BZ3UtilsComponent.deadZoneRandomRange(randomRangeNeg, randomRangePos, createDeadZoneNeg, createDeadZonePos), spawnPositionLineY, trans.position.z + BZ3UtilsComponent.deadZoneRandomRange(randomRangeNeg, randomRangePos, createDeadZoneNeg, createDeadZonePos));
		spawnRotationLine = transform.rotation;
		
		var goTMP : GameObject = Instantiate(groundLine, spawnPositionLine, spawnRotationLine);			
		goTMP.transform.position = spawnPositionLine;
		goTMP.transform.rotation = spawnRotationLine;
		
		var positions = new Array();
		var random : int = Random.Range(3, 6);
		
		for(var g : int = 0; g < random; g++){
			positions.Push(Vector3(Random.Range(-20, 20), 0.0, Random.Range(-20, 20)));
		}
		
		var lineRenderer : LineRenderer = goTMP.GetComponent(LineRenderer);
		lineRenderer.SetVertexCount(positions.length);
		
		for(var o : int = 0; o < positions.length; o++){
        	lineRenderer.SetPosition(o, positions[o]);
		}
		
		lineTransformList.Add(goTMP);
	}
}

public function RepositionLines(groundLine : GameObject, trans : Transform){
	groundLine.transform.position = Vector3(trans.position.x + BZ3UtilsComponent.deadZoneRandomRange(randomRangeNeg, randomRangePos, deadZoneNeg, deadZonePos), spawnPositionLineY, trans.position.z + BZ3UtilsComponent.deadZoneRandomRange(randomRangeNeg, randomRangePos, deadZoneNeg, deadZonePos));
	groundLine.transform.rotation = transform.rotation;
}